using System;
using System.IO;
using System.Web.UI;
using System.Web.UI.WebControls;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Entities.Users;
using DotNetNuke.Services.Exceptions;
using DotNetNuke.Services.Localization;
using Microsoft.VisualBasic;

namespace Code5Systems.FormMaster
{

	public partial class FormDataServiceSettings : DotNetNuke.Entities.Modules.PortalModuleBase
	{
		
#region  Web Form Designer Generated Code
		
		//This call is required by the Web Form Designer.
		[System.Diagnostics.DebuggerStepThrough()]private void InitializeComponent()
		{
			
		}
		protected System.Web.UI.HtmlControls.HtmlTable tblWsConfig;
		protected System.Web.UI.WebControls.Label lblHeading;
		protected System.Web.UI.WebControls.CheckBoxList cblAccessRoles;
		protected System.Web.UI.WebControls.LinkButton cmdUpdate;
		protected System.Web.UI.WebControls.TextBox txtWsUrl;
		
		//NOTE: The following placeholder declaration is required by the Web Form Designer.
		//Do not delete or move it.
		private System.Object designerPlaceholderDeclaration;
		
		private void Page_Init(System.Object sender, System.EventArgs e)
		{
			//CODEGEN: This method call is required by the Web Form Designer
			//Do not modify it using the code editor.
			InitializeComponent();
		}
		
#endregion
		
		
#region Private Members
		
		
		private DotNetNuke.Entities.Modules.PortalModuleBase _loaderCtl;
		private FormSettings objFormSettings;
		private string strOperationSuccess;
#endregion
		
		
#region Event Handlers
		
		private void Page_Load(System.Object sender, System.EventArgs e)
		{
			try
			{
				UserController objUsers = new UserController();
				UserInfo objUser = UserController.GetCurrentUserInfo();
				
				if (!(objUser.IsSuperUser || DotNetNuke.Security.PortalSecurity.IsInRole("Administrators")))
				{
					Response.Redirect("~/" + DotNetNuke.Common.Globals.glbDefaultPage);
				}
				
				this.LoaderCtl = (PortalModuleBase) this.Parent.Parent;
				
				string folderPath = Utility.GetAppPath();
				folderPath += "DesktopModules\\Code5Systems_FormMaster";
				string filePath = folderPath + "\\FormData.asmx";
				filePath = Server.MapPath(filePath);
				if (!File.Exists(filePath))
				{
					this.tblFileRename.Visible = true;
					this.tblWsConfig.Visible = false;
				}
				else
				{
					this.tblFileRename.Visible = false;
					this.tblWsConfig.Visible = true;
				}
				strOperationSuccess = Localization.GetString("OperationSuccess", this.LocalResourceFile);
				objFormSettings = Utility.GetFormSettings(ModuleId);
				this.lblHeading.Text = Localization.GetString("HeadingMsg", this.LocalResourceFile);
				
				if (!Page.IsPostBack)
				{
					BindData();
				}
				
				if ((!Request.UserAgent.Contains("MSIE")) || Request.Browser.Platform.Contains("Mac"))
				{
					if (DotNetNuke.Framework.AJAX.IsInstalled())
					{
						Utility.DisableAjax(this);
					}
				}
				
			}
			catch (Exception exc)
			{
				Exceptions.ProcessModuleLoadException(LoaderCtl, exc);
			}
		}

		protected void cmdUpdate_Click(System.Object sender, System.EventArgs e)
		{
			
			try
			{
				objFormSettings.WsAccessRoles = "";
				
				foreach (ListItem item in this.cblAccessRoles.Items)
				{
					if (item.Selected)
					{
						objFormSettings.WsAccessRoles += item.Text + ",";
					}
				}
				
				if (objFormSettings.WsAccessRoles.Length > 0)
				{
					objFormSettings.WsAccessRoles = objFormSettings.WsAccessRoles.Substring(0, objFormSettings.WsAccessRoles.Length - 1);
				}
				
				ModuleController objModules = new ModuleController();
				
				objModules.UpdateModuleSetting(ModuleId, "WsAccessRoles", objFormSettings.WsAccessRoles);
				
				DotNetNuke.UI.Skins.Skin.AddModuleMessage(this.LoaderCtl, strOperationSuccess, DotNetNuke.UI.Skins.Controls.ModuleMessage.ModuleMessageType.GreenSuccess);
				
			}
			catch (Exception exc)
			{
				Exceptions.ProcessModuleLoadException(LoaderCtl, exc);
			}
		}

		protected void cmdRenameAsmx_Click(object sender, EventArgs e)
		{
			string folderPath = Utility.GetAppPath();
			folderPath += "DesktopModules\\Code5Systems_FormMaster";
			string oldFilePath = folderPath + "\\FormData.asmx.config";
			string newFilePath = folderPath + "\\FormData.asmx";
			oldFilePath = Server.MapPath(oldFilePath);
			newFilePath = Server.MapPath(newFilePath);

			if (File.Exists(oldFilePath))
			{
				if (!File.Exists(newFilePath))
				{
					FileSystem.Rename(oldFilePath, newFilePath);
					this.tblFileRename.Visible = false;
					this.tblWsConfig.Visible = true;
				}
			}
		}

#endregion
		
		
#region Data Bind Methods
		
		private void BindData()
		{
			DotNetNuke.Security.Roles.RoleController oRolesCtl = new DotNetNuke.Security.Roles.RoleController();
			
			this.cblAccessRoles.DataSource = oRolesCtl.GetPortalRoles(PortalId);
			this.cblAccessRoles.DataBind();
			
			string[] aryRoles = null;
			
			if (objFormSettings.WsAccessRoles != string.Empty)
			{
				aryRoles = objFormSettings.WsAccessRoles.Split(',');
				
				for (int i = 0; i <= aryRoles.Length - 1; i++)
				{
					if (this.cblAccessRoles.Items.FindByText(aryRoles[i]) != null)
					{
						this.cblAccessRoles.Items.FindByText(aryRoles[i]).Selected = true;
					}
					
				}
			}
			
			this.txtWsUrl.Text = Utility.GetPortalAlias(PortalId) + "DesktopModules/Code5systems_FormMaster/FormData.asmx";
			
		}
		
#endregion
		
		
#region Public Properties
		public PortalModuleBase LoaderCtl
		{
			get
			{
				if (_loaderCtl == null)
				{
					_loaderCtl = this;
				}
				
				return _loaderCtl;
			}
			set
			{
				_loaderCtl = value;
			}
		}
#endregion

	}
	
}


